﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.下单MTO;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.UC.客户CRM.Service;
using DXApplicationTangche.UC.订单.service;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.销售记录.service;
using DXApplicationTangche.UC.项目成果管理.在制品管理.service;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrderShippedDTO = DXApplicationTangche.DTO.OrderShippedDTO;
using PlainMessageResult = DXApplicationTangche.UC.订单一览新.订单预览新.service.PlainMessageResult;

namespace DXApplicationTangche.UC.订单
{
    public partial class Form新建返修 : DevExpress.XtraEditors.XtraForm
    {
        private OrderShippedDTO orderShippedDTO;
        private String orderId;
        private String barcode;
        private QcItemsDTO DTO质检;
        private OrderCommitDTO返回 orderCommitDTO返回;
        private CustomerPDTO customerPDTO = new CustomerPDTO();
        private String mobile { get; set; }
        private VerifyVO verifyVO = new VerifyVO();

        public Form新建返修()
        {
            InitializeComponent();
            this.searchLookUpEdit店铺.Properties.DataSource = 销售记录Service.get店铺();
            this.searchLookUpEdit销售员.Properties.DataSource = 销售记录Service.get销售员();
            this.config读取店铺();
        }
        public Form新建返修(String barcode)
        {
            InitializeComponent();
            this.searchLookUpEdit店铺.Properties.DataSource = 销售记录Service.get店铺();
            this.searchLookUpEdit销售员.Properties.DataSource = 销售记录Service.get销售员();
            this.config读取店铺();
            this.barcode = barcode;
            this.textBox成衣条码.Text = barcode;
            try
            {
                this.orderCommitDTO返回 = 订单预览新Service.getOrderCommitDTOByBarcode(this.barcode);
                //this.customerPDTO =  CRM客户Service.getCustomerIntoByBarcode(this.barcode);
                this.DTO质检 = 在制品管理Service.getQcItemsByBarcode(this.barcode);
                if (String.IsNullOrEmpty(this.orderCommitDTO返回.orderId))
                {
                    MessageBox.Show("无查询信息");
                }
            }
            catch
            {
                MessageBox.Show("无查询信息");
            }
            this.显示();
            //this.textBox手机号查找0.Enabled = false;
            //this.simpleButton新建客户.Enabled = false;
            //this.simpleButton更多客户.Enabled = false;
            //this.simpleButton维护客户地址.Enabled = false;
        }
        private void config读取店铺()
        {
            if (System.IO.File.Exists(@"xml\config店铺.xml"))
            {
                DTO销售表头 dTO店铺 = DealXML.XMLFlieToObject<DTO销售表头>(@"xml\config店铺.xml", Encoding.UTF8);
                this.verifyVO.buildStore(dTO店铺.id, dTO店铺.name);
                this.searchLookUpEdit店铺.Text = this.verifyVO.storeName;
                this.searchLookUpEdit店铺.Enabled = false;
            }
        }
        #region 选店铺
        private void searchLookUpEdit店铺_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear店铺Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit店铺View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit店铺.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String storeId = this.searchLookUpEdit店铺.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    String storeName = this.searchLookUpEdit店铺.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    this.verifyVO.buildStore(storeId, storeName);
                    if (!System.IO.File.Exists(@"xml\config店铺.xml"))
                    {
                        DealXML.ObjectToXMLFile(new DTO销售表头(this.verifyVO.storeId, this.verifyVO.storeName), @"xml\config店铺.xml", Encoding.UTF8);
                    }
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear店铺Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit店铺.ToolTip = null;
            searchLookUpEdit店铺.EditValue = null;
        }
        private void searchLookUpEdit店铺_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.verifyVO.storeName;
        }
        #endregion
        #region 选销售员
        private void searchLookUpEdit销售员_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear销售员Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit销售员View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit销售员.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String assistantId = this.searchLookUpEdit销售员.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    String assistantName = this.searchLookUpEdit销售员.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    this.verifyVO.buildAssistant(assistantId, assistantName);
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear销售员Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit销售员.ToolTip = null;
            searchLookUpEdit销售员.EditValue = null;
        }
        private void searchLookUpEdit销售员_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.verifyVO.assistantName;
        }
        #endregion

        private void textBox成衣条码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.textBox成衣条码.Text.Trim()))
                {
                    return;
                }
                this.barcode = this.textBox成衣条码.Text.Trim();
                try
                {
                    this.orderCommitDTO返回 = 订单预览新Service.getOrderCommitDTOByBarcode(this.barcode);
                    //this.customerPDTO =  CRM客户Service.getCustomerIntoByBarcode(this.barcode);
                    this.DTO质检 = 在制品管理Service.getQcItemsByBarcode(this.barcode);
                    if (String.IsNullOrEmpty(this.orderCommitDTO返回.orderId))
                    {
                        MessageBox.Show("无查询信息");
                    }
                }
                catch
                {
                    MessageBox.Show("无查询信息");
                }
                this.显示();
            }
        }

        private void 显示()
        {
            this.richTextBox备注.Text = this.orderCommitDTO返回.remarks;
            this.textBox款式.Text = this.orderCommitDTO返回.styleNameCn;
            try { this.textBox尺码.Text = this.orderCommitDTO返回.styleSizeCd.Substring(15); } catch { this.textBox尺码.Text = ""; }
            this.textBox面料名称.Text = this.orderCommitDTO返回.materialNameCn;
            this.textBox面料编码.Text = this.orderCommitDTO返回.materialCode;
            this.pictureBox款式.LoadAsync(URL.图片url + URL.款式图片路径 + this.DTO质检.styleImagePath);
            this.pictureBox面料.LoadAsync(URL.图片url + URL.面料图片路径 + this.DTO质检.materialImagePath);
            this.textBox订单时间.Text = Convert.ToDateTime(this.orderCommitDTO返回.orderDate).ToString();
            this.textBox订单号.Text = this.orderCommitDTO返回.orderNo;
            this.orderId = this.orderCommitDTO返回.orderId;

            this.orderShippedDTO = FunctionHelper.JsonDeserialization<OrderShippedDTO>(
                RestCall.httpGetMethod(URL.接口url + "/rework/getOrderShippedInfoByOrderId?orderId=" + this.orderId
                    )
                );
            //this.loadAddress();
        }
        private void loadAddress()
        {
            this.mobile = this.customerPDTO.mobile;
            this.textBox客户姓名.Text = this.customerPDTO.customerName;
            this.textBox联系电话.Text = this.customerPDTO.mobile;
            this.textBox详细地址1.Text = this.customerPDTO.addressProvince
                + this.customerPDTO.addressCity 
                + this.customerPDTO.addressDistrict 
                + this.customerPDTO.addressDetail;
        }
        private void simpleButton确认返修_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.barcode))
            {
                MessageBox.Show("请扫描成衣码");
                return;
            }
            if (String.IsNullOrEmpty(this.richTextBox返修备注.Text.Trim()))
            {
                MessageBox.Show("请填写返修备注");
                return;
            }
            if (!this.verifyVO.build判空())
            {
                MessageBox.Show("请选择返修操作销售员及店铺");
                return;
            }
            //if (String.IsNullOrEmpty(this.mobile))
            //{
            //    MessageBox.Show("无返修客户信息");
            //    return;
            //}
            try
            {
                //String orderId =  订单Service.saveReworkOrder(this.barcode, this.richTextBox返修备注.Text.Trim(), this.mobile);
                String orderId =  
                    订单Service.saveReworkOrderStoreAndAssistant(
                        this.barcode.Trim(), 
                        this.richTextBox返修备注.Text.Trim(), 
                        this.mobile, 
                        this.verifyVO.storeId, 
                        this.verifyVO.assistantId
                        );
                if (String.IsNullOrEmpty(orderId))
                {
                    MessageBox.Show("返修开启失败");
                }
                else
                {
                    MessageBox.Show("返修开启成功");
                }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("返修开启失败" + ex.Message);
            }
            //PlainMessageResult PlainMessageResult =
            //    更改订单收货地址Service.post修改地址(
            //        this.orderId,
            //        this.textBox客户名.Text,
            //        this.textBox收件人姓名.Text,
            //        this.textBox手机号.Text,
            //        this.textBox省份.Text,
            //        this.textBox城市.Text,
            //        this.textBox县区.Text,
            //        this.textBox详细地址.Text,
            //        this.textBox邮编.Text
            //        );
            //if ("200".Equals(PlainMessageResult.errorCode))
            //{
            //    try
            //    {
            //        订单Service.saveRework(this.barcode, this.richTextBox返修备注.Text.Trim());
            //        MessageBox.Show("返修开启成功");
            //        this.Close();
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show("返修开启失败" + ex.Message);
            //    }
            //}
            //else
            //{
            //    MessageBox.Show("地址保存失败" + PlainMessageResult.errorMessage);
            //}
        }

        private void simpleButton更多客户_Click(object sender, EventArgs e)
        {
            new FormMTO客户一览(this.customerPDTO).ShowDialog();
            this.手机号查找信息并赋值(this.customerPDTO.mobile);
            this.textBox手机号查找0.Clear();
        }

        private void simpleButton新建客户_Click(object sender, EventArgs e)
        {
            new FormMTO添加客户地址(this.customerPDTO).ShowDialog();
            this.手机号查找信息并赋值(this.customerPDTO.mobile);
            this.textBox手机号查找0.Clear();
        }

        private void simpleButton维护客户地址_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.mobile))
            {
                MessageBox.Show("请选择要维护地址的客户");
                return;
            }
            new FormMTO添加客户地址(this.mobile).ShowDialog();
            this.手机号查找信息并赋值(this.mobile);
        }
        private void 手机号查找信息并赋值(String getMobile)
        {
            //根据电话号实例化对象并给textbox赋值
            this.mobile = getMobile;
            this.customerPDTO = CRM客户Service.getCustomerPByMobile(getMobile);
            this.textBox客户姓名.Text = this.customerPDTO.customerName;
            this.textBox联系电话.Text = this.customerPDTO.mobile;
            this.textBox详细地址1.Text =
                this.customerPDTO.addressProvince
                + this.customerPDTO.addressCity
                + this.customerPDTO.addressDistrict
                + this.customerPDTO.addressDetail;
        }

        private void textBox手机号查找0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.customerPDTO = MTO订单Service.getCustomerPByMobile(this.textBox手机号查找0.Text.Trim());
                if (String.IsNullOrEmpty(this.customerPDTO.mobile))
                {
                    MessageBox.Show("未创建用户");
                    return;
                }
                this.手机号查找信息并赋值(this.customerPDTO.mobile);
                this.textBox手机号查找0.Clear();
            }
        }
    }
    public class VerifyVO
    {
        public String storeId { get; set; }
        public String storeName { get; set; }
        public String assistantId { get; set; }
        public String assistantName { get; set; }
        public VerifyVO()
        {

        }

        public VerifyVO buildStore(String storeId, String storeName)
        {
            this.storeId = storeId;
            this.storeName = storeName;
            return this;
        }
        public VerifyVO buildAssistant(String assistantId, String assistantName)
        {
            this.assistantId = assistantId;
            this.assistantName = assistantName;
            return this;
        }
        public Boolean build判空()
        {
            if (String.IsNullOrEmpty(this.storeId)
                || String.IsNullOrEmpty(this.assistantId))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
